using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace OWED.Document
{
    /// <summary>
    /// This is the primary class for dealing with DocBook XML files.
    /// It is responsible for knowing everything about a document.
    /// 
    /// This class provides mostly helper methods for handling DocBook
    /// files in our system without having to deal with Xml for most 
    /// operations.
    /// </summary>
    public class DocumentBook
    {
        /// <summary>
        /// The ID of this document.
        /// </summary>
        private String documentName;

        /// <summary>
        /// The actual document.
        /// </summary>
        private XmlDocument xml;


        /// <summary>
        /// Constructs a DocumentBook given a document name
        /// and a xml representation.
        /// </summary>
        /// <param name="name">Name of the document</param>
        /// <param name="doc">Document to encapsulate</param>
        public DocumentBook(String name, XmlDocument doc)
        {
            documentName = name;
            xml = (XmlDocument) doc.Clone();
        }

        /// <summary>
        /// The unique name of this document
        /// </summary>
        public String Name
        {
            get { return documentName; }
        }

        /// <summary>
        /// Returns a CLONE of the XML data stored in this 
        /// dacument.  You are free to mutate it, but changes will
        /// not be represented in the Document.  
        /// 
        /// If you intend to mutate it and replace the document,
        /// construct a new document from the changed XML.
        /// </summary>
        public XmlDocument XML
        {
            get { return (XmlDocument)xml.Clone(); }
        }

        /// <summary>
        /// Returns the XmlNode associated with this disscussionID or
        /// null if discussionID is not found.
        /// 
        /// </summary>
        /// <param name="discussionID">The discussion ID to search for</param>
        /// <returns>The XmlNode associated with this discussion 
        /// or null if no node is found</returns>
        public XmlNode findDiscussion(String discussionID)
        {
            return recurseFindDiscussion(xml, discussionID);
        }

        /// <summary>
        /// Searches for a discussionID in all siblings and children
        /// of the node cur.  It recurses through children, then siblings.
        /// </summary>
        /// <param name="cur">Node to search</param>
        /// <param name="discussionID">discussionId to look for</param>
        /// <returns></returns>
        private XmlNode recurseFindDiscussion(XmlNode cur, String discussionID)
        {
            while (cur != null)
            {
                foreach (XmlAttribute attr in cur.Attributes)
                {
                    if (attr.Name == "commentID")
                    {
                        if (attr.Value == discussionID)
                        {
                            return cur;
                        }
                    }
                }

                //handle children
                foreach (XmlNode child in cur.ChildNodes)
                {
                    XmlNode result = recurseFindDiscussion(child, discussionID);
                    if (result != null)
                    {
                        return result;
                    }
                }

                //move to the next sibling
                cur = cur.NextSibling;
            }
            return null;
        }


        /// <summary>
        /// This method returns the message id associated with a
        /// XmlNode.  It allows you to avoid XmlParsing.
        /// </summary>
        /// <param name="node">The XmlNode to get the ID of</param>
        /// <returns>The ID, or null if no ID exists</returns>
        public static string GetMessageID(XmlNode node)
        {
            return null;
            //TODO: write me
        }

        /// <summary>
        /// This method returs the PC Data of a node, or null if there
        /// is no PC data.
        /// </summary>
        /// <param name="node">The Node to extract data from</param>
        /// <returns>The PC data, or null if not found</returns>
        public static string GetPCData(XmlNode node)
        {
            return null;
            //TODO: Write me
        }

        /// <summary>
        /// This will return a standard DocBook document.  That is, a file
        /// with all of our custom tags removed.  
        /// </summary>
        /// <returns>A version of the document with all our tags removed</returns>
        public XmlDocument GetCleanDocument()
        {
            return null;
            //TODO: Write me
        }


    }
}
